﻿Imports org.codegen.common.ProgressIndicator
Imports IICBusinessLogic.PeriodProcessing

Imports org.model.lib

Namespace DataConversion

    Public Class DASUpdate
        Implements IProgressable

        Private expectedDasHolders() As Integer = {4357, 4707, 2819, 3974, 4711, 4487, 4191, 4496, 4541, 4540, _
                                                   4426, 4795, 4542, 4612, 4486, 4382, 575, 4613, 4539, 4453, _
                                                   4380, 4259, 862, 4548, 4214, 4625, 4619, 4356, 4713, 1309, _
                                                   4504, 4631, 4739, 4397, 4406, 2335, 4147, 4084, 4523, 296, _
                                                   2068, 2043, 4528, 4420, 4702, 4741, 4490, 4492, 4464, 4680, _
                                                   4402}

        Private Property lesson735 As Lesson = LessonDataUtils.findOne("lessoncode='735'")
        Private Property lesson745 As Lesson = LessonDataUtils.findOne("lessoncode='745'")
        Private Property lesson765 As Lesson = LessonDataUtils.findOne("lessoncode='765'")

        Private lessonCodeReplacement As New Dictionary(Of String, Lesson)
        Private pers As Dictionary(Of Integer, Period)

        Sub New()

            lessonCodeReplacement.Add("560", lesson735)
            lessonCodeReplacement.Add("625", lesson745)
            lessonCodeReplacement.Add("610", lesson765)

        End Sub

        Public Sub reverseAndProcessDasPeriods()

            Me.reverseAllDASPeriods()
            Me.processDASPeriods()

        End Sub

        Public Sub reverseAllDASPeriods()

            Dim pp As PeriodProcess = Nothing
            Dim pps As List(Of PeriodProcess) = PeriodProcessDataUtils.findList( _
                "PeriodId in (select PeriodId from period where [DiplomaSuperTypeId]=5 ) order by PeriodProcessDate desc")

            pers = New Dictionary(Of Integer, Period)
            Dim i As Integer = 0

            For Each pp In pps

                Dim period As Period = PeriodDataUtils.findByKey(pp.PeriodId.GetValueOrDefault)
                i = i + 1

                RaiseEvent processGoing(Me, New ProgressableEventArgs( _
                                CInt(i / pps.Count * 100), pps.Count, _
                                       String.Format("Reversing period {0}, {1} of {2}", _
                                                     period.PeriodDescription, i, pps.Count)))



                If (period.IsOpen = False) Then
                    period.IsOpen = True
                    PeriodDataUtils.savePeriod(period)
                End If

                Dim preverse As PeriodProcessReverser = New PeriodProcessReverser(period)
                Console.WriteLine("reversing " & period.PeriodDescription)
                preverse.reversePeriodProcess()

                For Each c As PeriodClass In period.Classes
                    If lessonCodeReplacement.ContainsKey(c.Lesson.LessonCode) Then
                        c.LessonId = lessonCodeReplacement.Item(c.Lesson.LessonCode).LessonId
                    End If
                Next

                PeriodDataUtils.savePeriod(period)
                If pers.ContainsKey(period.PeriodId) = False Then
                    pers.Add(period.PeriodId, period)
                End If


            Next

            ModelContext.CurrentDBUtils.executeSQL("delete from [CertificationRequirement] where [CertificationTypeId] in (select CertificationTypeId from [CertificationType] where [DiplomaSuperTypeId]=5 and [CertificationType] like 'E%') ")
            ModelContext.CurrentDBUtils.executeSQL("delete from [CertificationType] where [DiplomaSuperTypeId]=5 and [CertificationType] like 'E%'")


        End Sub

        Sub processDASPeriods()

            Dim i As Integer = 0

            Dim periods As List(Of Period) = pers.Values.ToList
            periods.Sort(New PeriodComparer())
            Dim valsCount As Integer = periods.Count

            'process Period List
            For Each pr As Period In periods
                i = i + 1
                Dim bclosePer As Boolean = pr.PeriodDate.GetValueOrDefault < DateSerial(2013, 6, 1)

                RaiseEvent processGoing(Me, New ProgressableEventArgs( _
                               CInt(i / valsCount * 100), valsCount, _
                                      String.Format("Processing period {0}, {1} of {2}", _
                                                    pr.PeriodDescription, i, periods.Count)))

                Console.WriteLine("Processing period " & pr.PeriodDescription)
                Dim dc As PeriodProcessor = New PeriodProcessor(pr)
                dc.PeriodProcessRunDate = pr.PeriodDate
                dc.processPeriodAndStoreToDatabase()

                If bclosePer Then
                    pr.IsOpen = False
                End If
                PeriodDataUtils.savePeriod(pr)
            Next

        End Sub

#Region "IProgressable"

        Public Event processFinished(ByVal sender As Object, ByVal e As org.codegen.common.ProgressIndicator.ProgressableEventArgs) Implements org.codegen.common.ProgressIndicator.IProgressable.processFinished

        Public Event processGoing(ByVal sender As Object, ByVal e As org.codegen.common.ProgressIndicator.ProgressableEventArgs) Implements org.codegen.common.ProgressIndicator.IProgressable.processGoing

        Public Event processStart(ByVal sender As Object, ByVal e As org.codegen.common.ProgressIndicator.ProgressableEventArgs) Implements org.codegen.common.ProgressIndicator.IProgressable.processStart

        Public Property wasCancelled As Boolean Implements org.codegen.common.ProgressIndicator.IProgressable.wasCancelled
#End Region

        Public Sub startProcess() Implements IProgressable.startProcess

        End Sub
    End Class

    Public Class PeriodComparer
        Implements System.Collections.Generic.IComparer(Of Period)

        ''' <summary>
        ''' Sort Periods by Period Date,disploma Super Type, Asc
        ''' </summary>
        Public Function Compare(ByVal x As Period, ByVal y As Period) As Integer Implements System.Collections.Generic.IComparer(Of Period).Compare

            Dim ret As Integer = x.PeriodDate.GetValueOrDefault.CompareTo(y.PeriodDate.GetValueOrDefault)
            If ret = 0 Then
                ret = x.DiplomaSuperTypeId.GetValueOrDefault.CompareTo(y.DiplomaSuperTypeId.GetValueOrDefault)
            End If
            Return ret

        End Function
    End Class


End Namespace